{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hello Many Worlds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With Classiq (as perhaps in quantum) there are many parallel worlds one can work with. These refer to the IDE - Python SDK interfaces. In the following we run the same 'Hello World' example in different flows of the steps design-optimize-analyze-execute between the IDE and the Python SDK:\n",
    "\n",
    "1. [All in IDE](#all-in-ide)\n",
    "2. [Design (SDK) - Optimize (SDK) - Analyze (IDE) - Execute (SDK)](#design-sdk---optimize-sdk---analyze-ide---execute-sdk)\n",
    "3. [Design (SDK) - Optimize (SDK) - Analyze (IDE) - Execute (IDE)](#design-sdk---optimize-sdk---analyze-ide---execute-ide)\n",
    "\n",
    "The 'Hello World' example calculates in a superposition the simple arithmetic operation $y=x^2+1$ - an example that is covered in depth in the Classiq 101."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## All in IDE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copy and paste the following code in the `Model` tab in the IDE, then press `Synthesize` as the GIF belows shows:\n",
    "```\n",
    "qfunc main(output x: qnum, output y:qnum){\n",
    "  allocate<4>(x);\n",
    "  hadamard_transform(x);\n",
    "  y = x**2+1;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_design.gif\" />\n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check that you receive 2 blocks in the quantum circuit: `hadamard_transform` and `Arithmetic`, then press `Execute`:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_analyze.gif\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the `Execution` tab, choose the simulator under the Classiq provider (and make sure that other options are unmarked). Change the job name to be 'hello world' and press `Run`:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_execute.gif\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the `Jobs` tab check the results by hovering over the histogram bars and verifying you receive 16 bars, and each bar encapsulates the correct relation between $x$ and $y$, i.e. $y=x^2+1$:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_results.gif\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's it! You just completed designing, optimizing, running ans executing your first quantum algorithm with the Classiq IDE!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Design (SDK) - Optimize (SDK) - Analyze (IDE) - Execute (SDK)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Design your hello world quantum algorithm by running the following code in your favorite Python SDK environment (after installing Classiq):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:46.553734Z",
     "iopub.status.busy": "2024-05-07T13:26:46.553210Z",
     "iopub.status.idle": "2024-05-07T13:26:49.234186Z",
     "shell.execute_reply": "2024-05-07T13:26:49.233404Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import *\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(x: Output[QNum], y: Output[QNum]):\n",
    "\n",
    "    allocate(4, x)\n",
    "    hadamard_transform(x)  # creates a uniform superposition\n",
    "    y |= x**2 + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then run the following in order to optimize your algorithm from the Python SDK:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:49.239568Z",
     "iopub.status.busy": "2024-05-07T13:26:49.238089Z",
     "iopub.status.idle": "2024-05-07T13:26:51.744208Z",
     "shell.execute_reply": "2024-05-07T13:26:51.743588Z"
    }
   },
   "outputs": [],
   "source": [
    "quantum_program = synthesize(create_model(main))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Analyze your quantum circuit in the IDE by running the following code in Python and opening the popped up link:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:51.747107Z",
     "iopub.status.busy": "2024-05-07T13:26:51.746640Z",
     "iopub.status.idle": "2024-05-07T13:26:51.805964Z",
     "shell.execute_reply": "2024-05-07T13:26:51.805303Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/d6cbc39c-8ab1-4f86-8068-8ec37070673c?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "show(quantum_program)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_analyze.gif\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, execute your code from the Python SDK by running the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:51.843682Z",
     "iopub.status.busy": "2024-05-07T13:26:51.843461Z",
     "iopub.status.idle": "2024-05-07T13:26:53.166341Z",
     "shell.execute_reply": "2024-05-07T13:26:53.165580Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'x': 14.0, 'y': 197.0}: 84, {'x': 10.0, 'y': 101.0}: 76, {'x': 2.0, 'y': 5.0}: 74, {'x': 6.0, 'y': 37.0}: 70, {'x': 3.0, 'y': 10.0}: 68, {'x': 7.0, 'y': 50.0}: 66, {'x': 4.0, 'y': 17.0}: 65, {'x': 5.0, 'y': 26.0}: 65, {'x': 0.0, 'y': 1.0}: 64, {'x': 11.0, 'y': 122.0}: 55, {'x': 15.0, 'y': 226.0}: 54, {'x': 12.0, 'y': 145.0}: 54, {'x': 9.0, 'y': 82.0}: 53, {'x': 8.0, 'y': 65.0}: 53, {'x': 1.0, 'y': 2.0}: 53, {'x': 13.0, 'y': 170.0}: 46]\n"
     ]
    }
   ],
   "source": [
    "job = execute(quantum_program)\n",
    "results = job.result()[0].value.parsed_counts\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check that you receive 16 pairs of `{'x': , 'y': }` and that the values indeed follow the connection $y=x^2+1$. \n",
    "\n",
    "That's it! You just completed your first quantum algorithm from the Python SDK!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Design (SDK) - Optimize (SDK) - Analyze (IDE) - Execute (IDE)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's run the same example in a mixed flow of the previous two:\n",
    "\n",
    "Design your hello world quantum algorithm by running the following code in your favorite Python SDK environment (after installing Classiq):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:53.170798Z",
     "iopub.status.busy": "2024-05-07T13:26:53.170113Z",
     "iopub.status.idle": "2024-05-07T13:26:53.175824Z",
     "shell.execute_reply": "2024-05-07T13:26:53.175104Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import *\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(x: Output[QNum], y: Output[QNum]):\n",
    "\n",
    "    allocate(4, x)\n",
    "    hadamard_transform(x)  # creates a uniform superposition\n",
    "    y |= x**2 + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then run the following in order to optimize your algorithm from the Python SDK:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:53.180567Z",
     "iopub.status.busy": "2024-05-07T13:26:53.179353Z",
     "iopub.status.idle": "2024-05-07T13:26:55.343426Z",
     "shell.execute_reply": "2024-05-07T13:26:55.342788Z"
    }
   },
   "outputs": [],
   "source": [
    "quantum_program = synthesize(create_model(main))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Analyze your quantum circuit in the IDE by running the following code in Python and opening the popped up link:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:55.346589Z",
     "iopub.status.busy": "2024-05-07T13:26:55.346306Z",
     "iopub.status.idle": "2024-05-07T13:26:55.407362Z",
     "shell.execute_reply": "2024-05-07T13:26:55.406729Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/b24ddce6-083a-442c-8640-addf3aa354c7?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "show(quantum_program)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_analyze.gif\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check that you receive 2 blocks in the quantum circuit: `hadamard_transform` and `Arithmetic`, then press `Execute`:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_analyze.gif\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the `Execution` tab, choose the simulator under the Classiq provider (and make sure that other options are unmarked). Change the job name to be 'hello world' and press `Run`:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_execute.gif\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the `Jobs` tab check the results by hovering over the histogram bars and verifying you receive 16 bars, and each bar encapsulates the correct relation between $x$ and $y$, i.e. $y=x^2+1$:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center;\">\n",
    "    <img src=\"https://docs.classiq.io/resources/many_worlds_ide_results.gif\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's it! You just completed the hello world example in a mixed flow of IDE and Python SDK!\n",
    "\n",
    "You are ready to continue to the Classiq 101, enjoy!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:55.410285Z",
     "iopub.status.busy": "2024-05-07T13:26:55.409830Z",
     "iopub.status.idle": "2024-05-07T13:26:55.420273Z",
     "shell.execute_reply": "2024-05-07T13:26:55.419694Z"
    }
   },
   "outputs": [],
   "source": [
    "write_qmod(create_model(main), \"hello_many_worlds\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.11.7 ('classiq')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "529b62266d4f537a408698cf820854c65fe877011c7661f0f70aa11c4383fddc"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
